﻿using System;
using System.Collections.Generic;
//
using CookComputing.XmlRpc;
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI
{
   public class apiXmlRpc
   {
      internal static IMC _api;
      internal static IMC api
      {
         get
         {
            if ( _api == null )
            {
               CreateProxy();
            }
            return _api;
         }
      }

      // ------------------------------------------- constructor
      static apiXmlRpc()
      {
         CreateProxy();
      }
      // -----------------------------------------------------------------
      public static void CreateProxy()
      {
         CreateProxy( MCAPISettings.api_DataCenter, MCAPISettings.default_SecureAccess, MCAPISettings.default_Timeout );
      }
      public static void CreateProxy( string dataCenter, bool secureAccess, int timeout )
      {
         _api = null;
         _api = XmlRpcProxyGen.Create<IMC>();
         _api.UserAgent = MCAPISettings.api_UserAgent;
         _api.Timeout = timeout;
         //
         string protocol = "http://";
         if ( secureAccess ) protocol = "https://";
         // -------
         _api.Url = string.Format( "{0}{1}.{2}/", protocol, dataCenter, MCAPISettings.api_URL );
         // ----------------------------------------------------------
         // settings to reduce request size
         // ----------------------------------------------------------
         _api.UseStringTag = MCAPISettings.api_xmlrpc_UseStringTag;
         _api.Indentation = MCAPISettings.api_xmlrpc_Indentation;
         _api.EnableCompression = MCAPISettings.api_xmlrpc_EnableCompression;
      }
      // -----------------------------------------------------------------
      public static void DestroyProxy()
      {
         _api = null;
      }
      // -------------------------------------------------------- 
      internal static XmlRpcStruct FormatDictionaryToXmlRpcStruct( Dictionary<string, string> input )
      {
         XmlRpcStruct xrs = new XmlRpcStruct();
         foreach ( KeyValuePair<string, string> kv in input )
         {
            xrs.Add( kv.Key, kv.Value ?? "" );
         }
         return xrs;
      }

      internal static XmlRpcStruct FormatSegmentOptions( campaignSegmentOptions options )
      {
         XmlRpcStruct rpc = new XmlRpcStruct();
         if ( options.conditions.Count > 0 )
         {
            rpc.Add( "match", options.match );
            campaignSegmentCondition[] conditions = new campaignSegmentCondition[options.conditions.Count];
            int i = -1;
            foreach ( campaignSegmentCondition c in options.conditions )
            {
               i++;
               conditions[i] = new campaignSegmentCondition();
               conditions[i].field = c.field ?? "";
               conditions[i].op = c.op ?? "";
               conditions[i].value = c.value ?? "";
               conditions[i].extra = c.extra ?? "";
            }
            rpc.Add( "conditions", conditions );
         }
         return rpc;
      }

      internal static XmlRpcStruct FormatMemberMergeVars( Dictionary<string, object> merge_vars )
      {
         XmlRpcStruct MergeVars = new XmlRpcStruct();
         //
         foreach ( KeyValuePair<string, object> kv in merge_vars )
         {
            if ( kv.Key.ToLower().Contains( "groupings" ) )
            {
               XmlRpcStruct rpcStruct = new XmlRpcStruct();
               List<interestGroupings> igList = kv.Value as List<interestGroupings>;
               //
               int ix = -1;
               foreach ( interestGroupings ig in igList )
               {
                  ix++;
                  XmlRpcStruct igStruct = new XmlRpcStruct();
                  string groups = "";
                  if ( ig.groups.Count > 0 )
                     groups = apiHelper.MakeStringFromList( ig.groups );
                  if ( ig.id > 0 )
                     igStruct.Add( "id", ig.id );
                  if ( !string.IsNullOrEmpty( ig.name ) )
                     igStruct.Add( "name", ig.name );
                  igStruct.Add( "groups", groups );
                  rpcStruct.Add( ix.ToString(), igStruct );
               }
               //
               MergeVars.Add( kv.Key, rpcStruct );
            }
            else if ( kv.Value.GetType().Name.ToLower().Contains( "dictionary" ) )
            {
               XmlRpcStruct rpcStruct = new XmlRpcStruct();
               foreach ( KeyValuePair<string, object> kvp in kv.Value as Dictionary<string, object> )
               {
                  rpcStruct.Add( kvp.Key, kvp.Value );
               }
               MergeVars.Add( kv.Key, rpcStruct );
            }
            else if ( kv.Value.GetType().Name.ToLower().Contains( "datetime" ) )
            {
               DateTime dt = (DateTime)kv.Value;
               MergeVars.Add( kv.Key, apiHelper.FormatApiDateParm( dt ) );
            }
            else
               MergeVars.Add( kv.Key, kv.Value );
         }
         //
         return MergeVars;
      }
      // -----------------
      internal static XmlRpcStruct FormatCampaignOptions( campaignCreateOptions options )
      {
         XmlRpcStruct rpc = new XmlRpcStruct();
         if ( !string.IsNullOrEmpty( options.list_id ) )
            rpc.Add( "list_id", options.list_id );
         if ( !string.IsNullOrEmpty( options.subject ) )
            rpc.Add( "subject", options.subject );
         if ( !string.IsNullOrEmpty( options.from_email ) )
            rpc.Add( "from_email", options.from_email );
         if ( !string.IsNullOrEmpty( options.from_name ) )
            rpc.Add( "from_name", options.from_name );
         // change in Perceptive v1.2.5 
         // -- if omitted on empty string, MC default creates a to_email == *|FNAME|*
         rpc.Add( "to_email", options.to_email ?? "" );
         // ------------------------------------------------------------
         if ( options.template_id > 0 )
            rpc.Add( "template_id", options.template_id );
         if ( options.folder_id > 0 )
            rpc.Add( "folder_id", options.folder_id );
         rpc.Add( "tracking", options.tracking );
         if ( !string.IsNullOrEmpty( options.title ) )
            rpc.Add( "title", options.title );
         rpc.Add( "authenticate", options.authenticate );

         XmlRpcStruct anal = new XmlRpcStruct();
         foreach ( KeyValuePair<string, string> kv in options.analytics )
         {
            anal.Add( kv.Key, kv.Value ?? "" );
         }
         rpc.Add( "analytics", anal );
         rpc.Add( "auto_footer", options.auto_footer );
         rpc.Add( "inline_css", options.inline_css );
         rpc.Add( "generate_text", options.generate_text );
         rpc.Add( "auto_tweet", options.auto_tweet );
         rpc.Add( "timewarp", options.timewarp );
         rpc.Add( "ecomm360", options.ecomm360 );
         //
         return rpc;
      }

   }
}
